package com.atguigu.cms.controller;


import com.atguigu.cms.entity.CrmBanner;
import com.atguigu.cms.service.CrmBannerService;
import com.atguigu.common.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 后台banner管理
 * </p>
 *
 * @author Summer-day
 * @since 2020-07-30
 */
@RestController
@RequestMapping("/eduCms/bannerAdmin")
public class BannerAdminController {

    @Resource
    private CrmBannerService bannerService;

    @ApiOperation(value = "分页查询banner")
    @GetMapping("/pageBanner")
    public Result pageBanner(
            @ApiParam(name = "current", value = "当前页码数", required = true)
            @RequestParam("current") Integer current,
            @ApiParam(name = "size", value = "每页记录数", required = true)
            @RequestParam("size") Integer size){
        Page<CrmBanner> page = new Page<>(current,size);
        bannerService.page(page,null);
        return Result.ok().data("items",page.getRecords()).data("total",page.getTotal());
    }

    @ApiOperation(value = "添加banner")
    @PostMapping
    public Result addBanner(@RequestBody CrmBanner banner){
        bannerService.save(banner);
        return Result.ok();
    }

    @ApiOperation(value = "删除banner")
    @DeleteMapping("/{id}")
    public Result deleteBanner(@PathVariable("id")String id){
        bannerService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "更新banner")
    @PutMapping
    public Result updateBanner(@RequestBody CrmBanner banner){
        bannerService.updateById(banner);
        return Result.ok();
    }

    @ApiOperation(value = "获取banner")
    @GetMapping("/{id}")
    public Result getBanner(@PathVariable("id")String id){
        CrmBanner banner = bannerService.getById(id);
        return Result.ok().data("banner",banner);
    }

}

